package com.zmn.plat.admin.controller.channel.price.faultprice;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.admin.controller.channel.ChannelBaseController;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.ChannelFaultPriceVO;
import com.zmn.plat.model.entity.channel.tariff.ChannelTariff;

/**
 * 修改故障价格表
 *
 * @author xiangjunming
 * @since 2019/12/04 17:52:13
 */
@Controller
@RequestMapping("/channel/price/faultprice")
public class ChannelFaultPriceModifyController extends ChannelBaseController {

    /**
     * 绑定渠道故障价格表
     *
     * @param faultPrice
     *            the service price
     * @return the response dto
     */
    @RequestMapping(value = "bindingPriceTable")
    @ResponseBody
    public ResponseDTO bindingPriceTable(ChannelFaultPriceVO faultPrice) {
        Integer channelId = faultPrice.getChannelId();
        Integer bizType = faultPrice.getBizType();
        Integer tariffId = faultPrice.getTariffId();
        Integer tariffType = faultPrice.getTariffType();
        if (channelId == null) {
            return ResponseDTO.fail("channelId不能为空!");
        }
        if (bizType == null) {
            return ResponseDTO.fail("bizType不能为空!");
        }
        if (tariffId == null) {
            return ResponseDTO.fail("tariffId不能为空!");
        }
        if (tariffType == null) {
            return ResponseDTO.fail("tariffType不能为空!");
        }
        Channel channel = channelService.findByKey(channelId);
        if (channel == null) {
            return ResponseDTO.fail("渠道不存在!");
        }

        String staffName = getMcStaff().getStaffName();
        ChannelFaultPriceVO find =
            channelTariffService.findByChannelIdAndBizTypeAndTariffType(faultPrice.getChannelId(), faultPrice.getBizType(), tariffType);
        if (find == null) {
            faultPrice.setCreater(staffName);
            ChannelTariff channelTariff = new ChannelTariff();
            channelTariff.setChannelId(channelId);
            channelTariff.setTariffId(tariffId);
            channelTariff.setCreateTime(DateUtil.getNow());
            channelTariff.setCreater(staffName);
            channelTariff.setUpdateTime(DateUtil.getNow());
            channelTariff.setUpdater(staffName);
            channelTariffService.insert(channelTariff);
        } else {
            ChannelTariff channelTariff = new ChannelTariff();
            channelTariff.setRelateId(find.getRelateId());
            channelTariff.setTariffId(tariffId);
            channelTariff.setUpdateTime(DateUtil.getNow());
            channelTariff.setUpdater(staffName);
            channelTariffService.updateByKey(channelTariff);
        }
        return ResponseDTO.success();
    }
}
